package com.bone.payment.dao;

import java.util.List;
import java.util.Map;

import com.bone.common.BaseIbatisDAO;
import com.bone.constant.PayPaymentConstant;
import com.bone.page.Page;
import com.bone.payment.po.PayPayment;
import com.bone.title.po.Title;
import com.bone.workstation.po.Module;


/**
 * DAO.
 * @author liwenzhan
 *
 */
public class PayPaymentDAO extends BaseIbatisDAO {
	/**
	 * 取符合查询条件的交易记录个数.
	 * @param para
	 * @return
	 */
	public Long queryPaymentCountByParam(Map<String, Object> para){
		return (Long) getSqlMapClientTemplate().queryForObject("PAY_PAYMENT.queryPaymentCountByParam", para);
	}
	/**
	 * 取符合查询条件的交易记录数据.
	 * @param para
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<PayPayment> queryPaymentPageByParam(Map<String, Object> para){
		return getSqlMapClientTemplate().queryForList("PAY_PAYMENT.queryPaymentPageByParam",para);
	}
	/**
	 * 支付交易记录.
	 * @param parameter
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<PayPayment> queryPaymentListByParam(PayPayment payment){
		return getSqlMapClientTemplate().queryForList("PAY_PAYMENT.queryPaymentListByParam",payment);
	}
	
	/**
	 *   .
	 * @param payment
	 * @return
	 */
	public boolean insertPayment(PayPayment payment){
		getSqlMapClientTemplate().insert("PAY_PAYMENT.insert", payment);
		return true;
	}
	
	/**
	 * 
	 * @param payment
	 * @return
	 */
	public boolean updatePayment(PayPayment payment){
		getSqlMapClientTemplate().update("PAY_PAYMENT.update", payment);
		return true;
	}
	
	
	/**
	 * 按PK取PAYMENT
	 * @param paymentId
	 * @return
	 */
	public PayPayment selectByPaymentId(Long paymentId){
		return (PayPayment) super.getSqlMapClientTemplate().queryForObject("PAY_PAYMENT.queryByPaymentId", paymentId);
	}
	
	/**
	 * 根据对象NO支付交易记录.
	 * @param parameter
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<PayPayment> queryPaymentListByObjectNo(Long objectNo){
		return getSqlMapClientTemplate().queryForList("PAY_PAYMENT.queryPaymentListByObjectNo",objectNo);
	}
	
	/**
	 * 根据订单ID查询OrdPayment的记录数.
	 * @param objectNo
	 * @return
	 */
	public Long queryPaymentCountByObjectNo(final Long objectNo){
		@SuppressWarnings("unchecked")
		List<PayPayment> paymentList = getSqlMapClientTemplate().queryForList("PAY_PAYMENT.queryPaymentListByObjectNo",objectNo);
		return  (long) paymentList.size();
	}

	/**
	 * 查询出来需要通知而未通知的PAYMENT
	 * 
	 * @return
	 */
	public PayPayment selectUnNotifiedPayment() {
		PayPayment selectPayment = new PayPayment();
		selectPayment.setStatus(PayPaymentConstant.PAYMENT_SERIAL_STATUS.SUCCESS.name());
		selectPayment.setNotified("false");
		List<PayPayment> paymentList = queryPaymentListByParam(selectPayment);
		PayPayment payment = new PayPayment();
		if(paymentList != null && paymentList.size() > 0){
			payment = paymentList.get(0);
		}
		return payment;
		
	}

	
	/**
	 * 通过支付流ID查询一个支付流水记录.
	 * 
	 * @param paymentTradeNo
	 * @return
	 */
	public PayPayment selectByPaymentTradeNo(String paymentTradeNo) {
		PayPayment selectPayment = new PayPayment();
		selectPayment.setStatus(PayPaymentConstant.PAYMENT_SERIAL_STATUS.SUCCESS.name());
		selectPayment.setNotified("false");
		List<PayPayment> paymentList = queryPaymentListByParam(selectPayment);
		PayPayment payment = new PayPayment();
		if(paymentList != null && paymentList.size() > 0){
			payment = paymentList.get(0);
		}
		return payment;
	}
	
	
	
	 
	
}
